﻿using AyaWo.Entity;
using Commen;
using DAL;
using DbEntity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinService
{
    /// <summary>
    /// 同步计划任务实施
    /// </summary>
    public class TonbuDo
    {
        public void ToDo()
        {
            try
            {
                SpiderContentService scs = new SpiderContentService();
                //获取可以同步数据列表 循环判断是否都为0  排序value值 获取key值 替换title 为新的key值
                //复制需要用到的文件
                ///插入数据到news 更新内容表数据
                List<SpiderContent> lsc = scs.GetListCanTonBu();

                if (lsc == null || lsc.Count() == 0)
                    return;
                int i = 0;
                Random rand = new Random();
               
                foreach (SpiderContent q in lsc)
                {
                    if (i < 1)
                    {
                        if (!CopyFile(q.ID))
                        {
                            continue;
                        };
                        string ID = q.ID;
                        string title = q.Title;
                        string aTitle = null;
                        KKVModel kkv = new KKVModel();

                        List<KKVModel> lkkv = new List<KKVModel>();
                        lkkv.Add(new KKVModel { oldKey = q.key1, NewKey = q.getKey1, Value = q.getValue1 ?? 0 });
                        lkkv.Add(new KKVModel { oldKey = q.key2, NewKey = q.getKey2, Value = q.getValue2 ?? 0 });
                        lkkv.Add(new KKVModel { oldKey = q.key3, NewKey = q.getKey3, Value = q.getValue3 ?? 0 });
                        lkkv.Add(new KKVModel { oldKey = q.key4, NewKey = q.getKey4, Value = q.getValue4 ?? 0 });
                        lkkv.Add(new KKVModel { oldKey = q.key5, NewKey = q.getKey5, Value = q.getValue5 ?? 0 });
                        kkv = lkkv.OrderByDescending(x => x.Value).FirstOrDefault();
                        if (kkv != null && kkv.oldKey.NotNullOrWhiteSpace() && kkv.NewKey.NotNullOrWhiteSpace())
                        {
                            var gg = kkv.oldKey;
                            var nn = kkv.NewKey;
                            aTitle = q.Title.Replace(gg, nn);
                            NewsService.PilianChaRu(new News { ID = ID, Content = q.Content, CreatDate = DateTime.Now, Title = aTitle });
                        }
                        else
                        {
                            i = -1;
                        }
                    }
                    i++;
                }
            }
            catch (Exception ex)
            {
                Log.Error("TonbuDo : " + ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyID"></param>
        /// <returns></returns>
        public bool CopyFile(string keyID)
        {
            try
            {
                string oldPath = AppDomain.CurrentDomain.BaseDirectory + "\\Images\\" + keyID;
                Log.Info("oldPath ：" + oldPath);
                string newPath = ConfigurationManager.AppSettings["ImgPath"]?? @"C:\inetpub\web\Images";
                Log.Info("newPath ：" + newPath);

                if (newPath.IsNullOrWhiteSpace())
                {
                    return false;
                }
                else
                {
                    newPath = newPath + "\\" + keyID;
                    if (!DirFileHelper.IsExistDirectory(oldPath))
                        return false;
                    else
                    {
                        if (!DirFileHelper.IsExistDirectory(newPath))
                            DirFileHelper.CreateDir(newPath);
                        DirFileHelper.CopyFolder(oldPath,newPath);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("CopyFile : " + ex.Message);
                return false;
            }
        }
    }
}
